# Menu.py: Alien Xenocide menu
# Written by Victor Luna Freire - victorclf@live.com
# Copyright (c) Victor Luna Freire. All rights reserved.
# Sunday, June 24, 2009 00:00:00 PM
# Python 2.6.2
import pygame
import os

import Timer
import Input
import Defines
import Stars
import Sprites
import Gamescript
import Sound

class Text:
    def __init__(self, text, pos, font, effect_duration, color=(255, 255, 255), effect='explode'):
        self.text = text
        self.surface = font.render(text, True, color)
        self.pos = pos
        self.max_height = self.surface.get_height()
        self.max_width = self.surface.get_width()
        self.size = 0
        self.effect = effect
        self.effect_duration = effect_duration
        #slicescaled = pygame.transform.scale(self.slice_piece_temp, (1, slice_h))
       
    
            
    def current_pos(self, factor):
        center = Defines.SCREEN_CENTER
        xdiff = abs(self.pos[0] - Defines.SCREEN_CENTER[0])
        xposvar = int(factor * xdiff)
        currentposx = Defines.SCREEN_CENTER[0]
        currentposx += xposvar if self.pos[0] > center[0] else -xposvar
        
        ydiff = abs(self.pos[1] - Defines.SCREEN_CENTER[1])
        yposvar = int(factor * ydiff)
        currentposy = Defines.SCREEN_CENTER[1]
        currentposy += yposvar if self.pos[1] > center[1] else -yposvar
        return (currentposx, currentposy)
        
        
    def current_size(self, factor):
        return (int(self.max_width * factor), int(self.max_height * factor))
        
        
    def draw(self, screen, elapsed):
        if elapsed < self.effect_duration:
            factor = elapsed / float(self.effect_duration)
            
            currentpos = self.current_pos(factor)
            currentsize = self.current_size(factor)
            
            textscaled = pygame.transform.scale(self.surface, currentsize)
            screen.blit(textscaled, currentpos)
            
        else:
            screen.blit(self.surface, self.pos)
                

class Frame:
    def __init__(self, texts, duration):
        self.duration = duration
        self.texts = texts
        self.reset()
    
    
    def reset(self):
        self.elapsed = 0
        
    
    def finished(self):
        return self.elapsed >= self.duration
    
    
    def main(self, screen, time_var):
        self.elapsed += time_var
        for t in self.texts:
            if self.elapsed > t[0]:
                t[1].draw(screen, self.elapsed - t[0])
                
                

class Menu:
    def __init__(self):
        # Initializes the main menu objects.
        self.stars = Stars.Stars(Defines.SCREEN_WIDTH, Defines.SCREEN_HEIGHT, True)
        #self.menu_script = MenuScript()
        self.afont_biggest = pygame.font.Font('ALIEN5.TTF', 42)
        self.afont_bigger = pygame.font.Font('ALIEN5.TTF', 36)
        self.afont_big = pygame.font.Font('ALIEN5.TTF', 30)
        self.afont_medium = pygame.font.Font('ALIEN5.TTF', 25)
        self.frames = []
        
        frame1texts = [(1000, Text('criado por: ', (70, 160), self.afont_bigger, 1000)),
                       (3000, Text('victor da cunha luna freire', (100, 210), self.afont_big, 1000))]
                       
        
        frame2texts = [(500, Text('para a disciplina de: ', (70, 160), self.afont_bigger, 1000)),
                       (2500, Text('teoria dos Grafos - 2009.2', (100, 210), self.afont_big, 1000))]
        
        frame3texts = [(1000, Text('professor: ', (70, 160), self.afont_bigger, 1000)),
                       (4000, Text('jorge abrantes', (100, 210), self.afont_big, 1000))]
        
        frame4texts = [(1500, Text('graphaze', (253, 213), self.afont_bigger, 1000))]
        
                       
        
                               
                        
        self.frames = [Frame(frame4texts, 8000), (Frame(frame1texts, 8000)), Frame(frame2texts, 8000), Frame(frame3texts, 8000)]
        self.current_frame = 0
        
        self.space_to_play = self.afont_medium.render('pressione espaco para jogar', True, (72, 72, 255))
        self.difficulty_choose = self.afont_big.render('Escolha o tamanho do labirinto: ', True, (255, 255, 255))
        self.difficulties = [self.afont_big.render('A) Muito Pequeno (%d x %d)' % Defines.SIZES[0], True, (255, 255, 255)),
                             self.afont_big.render('B) Pequeno (%d x %d)' % Defines.SIZES[1], True, (255, 255, 255)),
                             self.afont_big.render('C) Normal (%d x %d)' % Defines.SIZES[2], True, (255, 255, 255)),
                             self.afont_big.render('D) Grande (%d x %d)' % Defines.SIZES[3], True, (255, 255, 255)),
                             self.afont_big.render('E) Muito Grande (%d x %d)' % Defines.SIZES[4], True, (255, 255, 255)),
                             self.afont_big.render('F) Gigante! (%d x %d)' % Defines.SIZES[5], True, (255, 255, 255))]
        
    
    
    def reset_menu(self):
        self.choosing_difficulty = False
        pygame.mixer.music.set_volume(1.0)
        self.current_frame = 0
        for f in self.frames: f.reset()
    
    
    def main_menu_input(self, mult_factor):
        keep_running = Input.Input.get_input()
                
        if Input.Input.check_once(pygame.K_ESCAPE):
            if self.choosing_difficulty:
                self.choosing_difficulty = False
            else:
                keep_running = False
            
        if Input.Input.check_once(pygame.K_SPACE):
            if not self.choosing_difficulty:
                self.choosing_difficulty = True
                pygame.mixer.music.set_volume(0.5)
                Gamescript.SoundDirector.sound_event('mapsize')
        
        # Difficulty selector
        if self.choosing_difficulty:
            for choose_key in xrange(pygame.K_a, pygame.K_a + Defines.PREDEFINED_SIZES):
                if Input.Input.check_once(choose_key):
                    self.new_builder(*Defines.SIZES[choose_key - pygame.K_a])
        
        
        return keep_running
    
    
    def credits(self):
        self.frames[self.current_frame].main(self.screen, self.time_variation)
        
        if self.current_frame < len(self.frames):
            if self.frames[self.current_frame].finished():
                self.current_frame += 1
                if self.current_frame == len(self.frames):
                    self.current_frame = 0
                    for f in self.frames: f.reset()

    
    def main_menu(self):
        if not self.main_menu_input(self.mult_factor): return False
        
        self.screen.fill((0, 0, 0))
        
        self.stars.main(self.screen, self.mult_factor)
        self.screen.blit(Sprites.Sprites.get_sprite('menu\\logo'), (30,40))        
        
        if not self.choosing_difficulty:
            self.screen.blit(self.space_to_play, (170,445))
            self.credits()
        else:
            self.screen.blit(self.difficulty_choose, (70, 150))
            for i in xrange(len(self.difficulties)):
                self.screen.blit(self.difficulties[i], (100, 165 + (i + 1) * 40))
                
               
        pygame.display.flip()
        return True
    
